/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package seso;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.RoundRectangle2D;
import java.awt.GradientPaint;
import java.awt.event.KeyEvent;

/**
 *
 * @author Administrator
 */
public class RHomeStartAndSearch extends javax.swing.JTextField {

    HomePage homePage = null; 
            
    javax.swing.JButton cmdStart, cmdClear;
    
    private boolean focus_Gained = false, mouse_Entered = false;
    private final int arcWH = 8;
    private String strStartAndSearch = "Start And Search";
    private String oldProgramID = "";
    private String curProgramID = "";
    

    public RHomeStartAndSearch() {
        super();

        initComponent();
        addListener();
    }
    
    public void setHomePage(HomePage homePage) {
        this.homePage = homePage;
    }

    private void initComponent() {
        setOpaque(false);
        setText(strStartAndSearch);
        setMargin(new java.awt.Insets(1, 4, 1, 40));
        setFont(new Font("Arial", Font.ITALIC, 12));
        setForeground(new Color(60,60,60));
    }
    
    public void setOldProgramID(String oldProgramID){
        this.oldProgramID = oldProgramID;
    }
    
    public String  getCurProgramID() {
        return curProgramID;
    }
    public void setCurProgramID(String curProgramID) {
        this.curProgramID = curProgramID;
    }
    
    public void setRelatedButton(javax.swing.JButton cmdStart, javax.swing.JButton cmdClear) {
        this.cmdStart = cmdStart;
        this.cmdClear = cmdClear;
    }
    

    private void addListener() {

        addFocusListener(new java.awt.event.FocusAdapter() {

            public void focusGained(java.awt.event.FocusEvent evt) {
                setFont(new Font("Arial", Font.PLAIN, 12));
                setForeground(Color.BLACK);
                setText(oldProgramID);
                focus_Gained = true;
                repaint();

            }

            public void focusLost(java.awt.event.FocusEvent evt) {
                setFont(new Font("Arial", Font.ITALIC, 12));
                setForeground(new Color(60,60,60));
                setText(strStartAndSearch);
                focus_Gained = false;
                repaint();
            }
        });

        addMouseListener(new java.awt.event.MouseAdapter() {

            public void mouseEntered(java.awt.event.MouseEvent evt) {
                mouseEnteredAction();
                mouse_Entered = true;
                repaint();
            }

            public void mouseExited(java.awt.event.MouseEvent evt) {
                mouseExitedAction();
                mouse_Entered = false;
                repaint();
            }
        });

        addKeyListener(new java.awt.event.KeyAdapter() {

            public void keyTyped(java.awt.event.KeyEvent evt) {
                KeyTypedAction(evt);
            }
        });

    }
    
    
    private void mouseEnteredAction() {
        setFocusable(true);
    }
    
    private void mouseExitedAction() {
//        setFocusable(false);
          if (!getText().equals(strStartAndSearch)) {
          oldProgramID = getText();
          curProgramID = getText();
          }
   }
    
    
    

    private void KeyTypedAction(java.awt.event.KeyEvent evt) {
        switch (evt.getKeyChar()) {
            case KeyEvent.VK_ENTER:
                oldProgramID = getText();
                curProgramID = getText();
                homePage.cmdStartAndStartAction();
                break;
                
            case KeyEvent.VK_ESCAPE:
                setText("");
                oldProgramID = "";
                curProgramID = "";
                break;
        }
    }

    @Override
    protected void paintComponent(Graphics g) {

        Graphics2D g2d = (Graphics2D) g;
        int width = getWidth();
        int height = getHeight();

        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        RoundRectangle2D.Double rect = new RoundRectangle2D.Double(0, 0, width, height, arcWH, arcWH);
        g2d.setClip(rect);

        if (focus_Gained) {
            GradientPaint gp = new GradientPaint(0.0F, 0.0F, new Color(230, 230, 230), 0.0F, height / 2, new Color(255, 255, 255), false);
            g2d.setPaint(gp);
            g2d.fillRoundRect(1, 1, width - 2, height - 2, arcWH, arcWH);
        }

        if (!focus_Gained && mouse_Entered) {
            GradientPaint gp = new GradientPaint(0.0F, 0.0F, new Color(114, 155, 68), 0.0F, height, new Color(173, 216, 128), false);
            g2d.setPaint(gp);
            g2d.fillRoundRect(1, 1, width - 2, height - 2, arcWH, arcWH);
        }

        super.paintComponent(g);
        
                if (cmdStart != null)
                    cmdStart.setVisible(focus_Gained);
                if (cmdClear != null)
                    cmdClear.setVisible(focus_Gained);        
    }

    @Override
    protected void paintBorder(java.awt.Graphics g) {
        RoundRectangle2D.Double rect;
        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        int width = getWidth();
        int height = getHeight();

        if (focus_Gained) {
            g2d.setColor(new Color(255, 255, 255));
            rect = new RoundRectangle2D.Double(1, 1, width - 3, height - 3, arcWH, arcWH);
            g2d.draw(rect);
        } else if (mouse_Entered) {
            g2d.setColor(new Color(65, 101, 26));
            rect = new RoundRectangle2D.Double(1, 1, width - 3, height - 3, arcWH, arcWH);
            g2d.draw(rect);
        } else {
            g2d.setColor(new Color(0, 0, 0));
            rect = new RoundRectangle2D.Double(1, 1, width - 3, height - 3, arcWH, arcWH);
            g2d.draw(rect);
            g2d.setColor(new Color(86, 130, 37));
            rect = new RoundRectangle2D.Double(0, 0, width - 1, height - 1, arcWH, arcWH);
            g2d.draw(rect);
       }

    }
}
